//
//  ConsultViewController.m
//  soccer
//
//  Created by 上官千城 on 15/9/2.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OConsultViewController.h"

@interface OConsultViewController ()

@end

@implementation OConsultViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =   [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self getInformation];
    }
    return self;
}

-(void)creatCommetButton:(NSIndexPath *)indexPath
{
    UIButton *publish = [UIButton buttonWithType:UIButtonTypeCustom];
    publish.frame = CGRectMake(WIDTH/2 - 50, 270 , 40*Proportion, 40*Proportion);
    [publish addTarget:self action:@selector(publishCommentAction:) forControlEvents:UIControlEventTouchUpInside];
//    publish.backgroundColor = [UIColor cyanColor];
    publish.tag = 10000+indexPath.section;
    [publish setImage:[UIImage imageNamed:@"manpage_11-02@2x"] forState:UIControlStateNormal];
    [self.consultCell addSubview:publish];
    
    //点赞
    self.agree = [UIButton buttonWithType:UIButtonTypeCustom];
    self.agree.frame = CGRectMake(publish.frame.origin.x + publish.frame.size.width+10 , publish.frame.origin.y , 40 * Proportion, 40 * Proportion);
    [self.agree addTarget:self action:@selector(agreeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.agree setImage:[UIImage imageNamed:@"manpage_11@2x"] forState:UIControlStateNormal];
    //    self.agree.backgroundColor = [UIColor cyanColor];
    [self.consultCell addSubview:self.agree];
    
    //点赞数
    self.goodNum = [[UILabel alloc]initWithFrame:CGRectMake(self.agree.frame.origin.x+self.agree.frame.size.width+10, self.agree.frame.origin.y+10, 50*Proportion, 20)];
    //    self.goodNum.backgroundColor = [UIColor orangeColor];
    self.goodNum.font = [UIFont systemFontOfSize:18*Proportion];
    [self.consultCell addSubview:self.goodNum];

}

-(void)creatView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //取消换航线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    
    //评论详情表
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    self.backView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.backView];
    
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, HEIGHT)];
    aView.backgroundColor = [UIColor clearColor];
    [self.backView addSubview:aView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackView:)];
    [aView addGestureRecognizer:tap];
    
    self.commentTableView = [[UITableView alloc]initWithFrame:CGRectMake(60, -20, WIDTH - 60, HEIGHT) style:UITableViewStyleGrouped];
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    self.backView.layer.shadowOffset = CGSizeMake(0, 3);
    self.backView.layer.shadowOpacity = 0.5;
    self.backView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.backView.layer.shadowRadius = 5.0;
    //    self.commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.backView addSubview:self.commentTableView];
    //滑动手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
    //    swipe.delegate = self;
    [self.commentTableView addGestureRecognizer:swipe];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"资讯";
    
    
    
    [self creatView];
    [self creatCommentText];
//    [self getInformation];
    
    
}

-(void)getInformation
{
    //获取资讯列表初始化
    self.OGetinfor = [[OGetInformation alloc]initWithCallBack:@selector(OGetinfor:) for:self];
    
    //获取评论初始化
    self.OGetInforComment = [[OGetInforComment alloc]initWithCallBack:@selector(OGetInforComment:) for:self];
    
    //评论资讯
    self.OAddinfoComment = [[OAddInforComment alloc]initWithCallBack:@selector(OAddinfoComment:) for:self];
    OUserInfo *user = [OUserInfo getInfo];
    
    self.OGetinfor.userId = user.userId;
    self.OGetinfor.n = 20;
    self.OGetinfor.t = 0;
    NSLog(@"getinformation...%ld",user.userId);
    [self.OGetinfor request];
    
    
}

-(void)OAddinfoComment:(OBaseLogic *)OAddInforComment
{

    self.addComment = [[OConcom alloc]init];
    NSDictionary *dic  = [NSDictionary dictionary];
    dic = self.OAddinfoComment.dataDic;
    self.addComment.nickName = [dic objectForKey:@"nickName"];
    self.addComment.content = [dic objectForKey:@"content"];
    self.addComment.agreeNum = [[dic objectForKey:@"agreeNum"]intValue];
    self.addComment.time = [dic objectForKey:@"time"];
    self.addComment.informationId = [[dic objectForKey:@"informationId"]longValue];
    
//    NSLog(@"befor commentIndex %@",self.commentIndex);
    
    NSMutableDictionary *temDic = [NSMutableDictionary dictionary];
    for (int i = 0; i < self.infoIndex.count; i++) {
//        OConcom *temDic = [[OConcom alloc]init];
        [temDic setValue:self.commentIndex[i] forKey:self.infoIndexArray[i]];
        
        for (NSString *key in temDic.allKeys) {
            if ([key longLongValue] == self.addComment.informationId ) {
                NSLog(@"%@",key);
                [self.commentIndex[i] addObject:self.addComment];
                [self.tableView reloadData];
                return;
//                [temDic setValue:self.addComment forKey:key];
            }
            
        }
    }
    
//    NSLog(@"temDic = %@",temDic);
//    NSLog(@"after commentIndex %@",self.commentIndex);
//
    
//    for (NSMutableArray *arr in self.commentIndex) {
//        for (OConcom *temCom in arr) {
//            NSLog(@" tem  %ld com  %ld ",temCom.informationId,self.addComment.informationId);
//            if (temCom.informationId == self.addComment.informationId) {
//                NSLog(@"%@",self.addComment.content);
//                self.OGetInforComment.inforIds = [NSString stringWithFormat:@"%ld",self.addComment.informationId];
//                [arr addObject:temCom];
//                [self.OGetInforComment request];
//            }
//        }
//    }
//    NSLog(@"%@",self.commentIndex);


    
    
 
    
}

-(void)OGetInforComment:(OBaseLogic *)OInforComment
{
    
    self.getInforCommentArray = [NSMutableArray array];
    self.commentIndexArray = [NSMutableArray array];
    self.commentIndex = [NSMutableArray array];
//    for (int i = 0; i < self.OGetInforComment.array.count; i++) {
        for (NSMutableDictionary *dic in self.OGetInforComment.array) {
            NSMutableDictionary *commentDic = [NSMutableDictionary dictionary];
            commentDic = [dic objectForKey:@"comment"];
                OConcom *com = [[OConcom alloc]init];
                com.agreeNum = [[commentDic objectForKey:@"agreeNum"]intValue];
                com.commentId = [[commentDic objectForKey:@"commentId"]longValue];
                com.content = [commentDic objectForKey:@"content"];
                com.disagreeNum = [[commentDic objectForKey:@"disagreeNum"]intValue];
                com.fatherComentId = [[commentDic objectForKey:@"fatherComentId"]longValue];
                com.nickName = [commentDic objectForKey:@"nickName"];
                com.informationId = [[commentDic objectForKey:@"informationId"]longValue];
                com.time = [commentDic objectForKey:@"time"];
                com.userId = [[commentDic objectForKey:@"userId"]longValue];
//                NSLog(@"%@",com.content);
                [self.commentIndexArray addObject:[NSString stringWithFormat:@"%ld",com.informationId]];
          
            [self.getInforCommentArray addObject:com];

//            NSLog(@"arrrrrr = %@",self.arr);
    
            
//        }
    }
  

//    NSLog(@"con %ld,com  %ld",self.infoArray.count,self.getInforCommentArray.count);
    for (OConsult *con in self.infoArray) {
        self.arr = [NSMutableArray array];
        for (OConcom *com in self.getInforCommentArray) {
            if (con.informatinId == com.informationId) {
//                NSLog(@"con  %ld , com %@",con.informatinId,com.content);
                [self.arr addObject:com];
            }
        }
        [self.commentIndex addObject:self.arr];
        
    }
       [self.tableView reloadData];


}

-(void)OGetinfor:(OBaseLogic *)OGetinfor
{
    self.infoArray = [NSMutableArray array];
    self.infoIndexArray = [NSMutableArray array];
    self.infoIndex = [NSMutableArray array];
    for (NSMutableDictionary *dic in  self.OGetinfor.dataDic) {
        OConsult *con = [[OConsult alloc]init];
        con.address = [dic objectForKey:@"address"];
        con.agreeNum = [[dic objectForKey:@"agreeNum"]intValue];
        self.agreeNum = con.agreeNum;
        con.disagreeNum = [[dic objectForKey:@"disagreeNum"]intValue];
        con.heat = [[dic objectForKey:@"heat"]intValue];
        con.icon = [dic objectForKey:@"icon"];
        con.informatinId = [[dic objectForKey:@"informationId"]longValue];
        con.time = [[dic objectForKey:@"time"]longValue];
        con.timeShow = [dic objectForKey:@"timeShow"];
        con.type = [[dic objectForKey:@"type"]intValue];
        con.userId = [[dic objectForKey:@"userId"]integerValue];
        con.pictureAddressList = [dic objectForKey:@"pictureAddressList"];
        con.nickName = [dic objectForKey:@"nickName"];
        [self.infoArray addObject:con];
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:[self.infoArray lastObject]];
        [self.infoIndex addObject:arr];
        
        [self.infoIndexArray addObject:[NSString stringWithFormat:@"%ld",con.informatinId]];
//        self.OGetInforComment.inforIds = [NSString stringWithFormat:@"%@,%ld",self.OGetInforComment.inforIds,con.informatinId];
        
        
            }
    self.OGetInforComment.inforIds = [self.infoIndexArray firstObject];
    for (int i = 1; i < self.infoIndexArray.count; i++) {
        self.OGetInforComment.inforIds = [NSString stringWithFormat:@"%@,%@",self.OGetInforComment.inforIds,self.infoIndexArray[i]];
    }
    
//    self.OGetInforComment.inforIds = [NSString stringWithFormat:@"%@",self.infoIndexArray];
    [self.OGetInforComment request];
    

//    [self.tableView reloadData];
//    NSLog(@"%@",self.infoIndexArray);
    
    NSLog(@"get Commnet...");
//    self.OGetInforComment.inforId = self.OGetinfor.informatinId;
    
}

//滑动评论详情回滚返回主页
-(void)swipeAction
{
    [UIView animateWithDuration:0.3f animations:^{
        self.tableView.frame = CGRectMake(0, -20, WIDTH, HEIGHT);
        
        self.backView.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
        
    }];
}

//点击评论详情回滚返回主页
-(void)tapBackView:(UITapGestureRecognizer *)sender
{
    [UIView animateWithDuration:0.3f animations:^{
        self.tableView.frame = CGRectMake(0, -20, WIDTH, HEIGHT);
        self.backView.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
        
    }];
}


//tableview协议方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        if ( indexPath.row == 0) {
            static NSString *info = @"consultCell";
            self.consultCell = [tableView cellForRowAtIndexPath:indexPath];
            if (self.consultCell == nil) {
                self.consultCell = [[OConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:info];
            }
            self.consultCell.selectionStyle = UITableViewCellSelectionStyleNone;
//            OConsult *con = self.infoIndex[indexPath.section][indexPath.row];
            OConsult *con = [self.infoArray objectAtIndex:indexPath.section];
            self.consultCell.con = con;
//            self.inforId = con.informatinId;
            NSLog(@"资讯===%ld",con.informatinId);
            
            self.consultCell.myDelgate = self;
            self.consultCell.imageDelegate = self;
//            if (self. != 0) {
                [self creatCommetButton:indexPath];
                self.agreeNum = con.agreeNum;
//            }
            
            
            
 
//            NSLog(@"%ld",self.OGetInforComment.inforId);
          
 
            return self.consultCell;
        }
        if (indexPath.row != 0) {
            static NSString *cellInfo = @"CommentCell";
            OConsultCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
            if (cell1 == nil) {
                cell1 = [[OConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfo];
            }
            cell1.selectionStyle = UITableViewCellSelectionStyleNone;
//                    NSLog(@"%@",self.getInforCommentArray);
            if ( self.getInforCommentArray.count!= 0) {
//                OConcom *com = [self.getInforCommentArray objectAtIndex:indexPath.row-1];
                OConcom *com = self.commentIndex[indexPath.section][indexPath.row-1];
                cell1.com = com;

                NSLog(@"评论 =====%ld",com.informationId);
//                NSLog(@"=--%@",com.content);
//                [self creatAgreeComment];

            }
        
            cell1.myDelegate = self;
            return cell1;
        }
    }
    if (tableView == self.commentTableView) {
        static NSString *cellinfo = @"RightCommentCell";
        OConsultCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellinfo];
        if (cell2 == nil) {
            cell2 = [[OConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellinfo];
        }
        if (self.comArray.count != 0) {
            OConcom *com = self.comArray[indexPath.section];
            cell2.com = com;
        }
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell2;
    }
    
    return nil;
}
//点击Cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.tableView) {
        if (indexPath.row == 0) {
            OConsultDetailsController *view = [[OConsultDetailsController alloc]init];
            //隐藏tabelbar & 隐藏nav
            view.con = [self.infoArray objectAtIndex:indexPath.section];
            view.hidesBottomBarWhenPushed = YES;
            self.navigationController.navigationBarHidden = YES;
            [self.navigationController pushViewController:view animated:YES];
        }
        if (indexPath.row != 0) {
//            [UIView animateWithDuration:0.3f animations:^{
//                self.tableView.frame = CGRectMake(-60, -20, WIDTH, HEIGHT);
//                
//                self.backView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
//                
//            }];
            
        }
    }
    if (tableView == self.commentTableView) {
        NSLog(@"点击方法");
    }
    
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (section) {
//        return self.arr.count + 1;
//    }
    NSMutableArray *array = [self.commentIndex objectAtIndex:section];
    
    return array.count + 1;
}
//区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView) {
        return self.infoArray.count;
    }
    return 1;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        if (indexPath.row == 0) {
          
            
            
            

            return 320;
            
        }
        if (indexPath.row != 0) {
            return 150;
        }
    }
    if (tableView == self.commentTableView) {
        
    }
    return 500;
}
//点赞
-(void)goodAction
{
    NSLog(@"点赞");
}



//创建评论View
-(void)creatCommentText
{
    self.open = NO;
    
    self.commentView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT, WIDTH, HEIGHT-70)];
    self.commentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.commentView];
    
    //底层view 轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeAction)];
    [self.commentView addGestureRecognizer:tap];
    
    //    UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, WIDTH - 20, HEIGHT/3)];
    //    text.textAlignment = NSTextAlignmentJustified;
    //    text.placeholder = @"请输入评论信息";
    //    text.borderStyle = UITextBorderStyleRoundedRect;
    //    [self.commentView addSubview:text];
    
    self.texView = [[UITextView alloc] initWithFrame:CGRectMake(10, 100, WIDTH - 20, HEIGHT/3)];
    self.texView.layer.cornerRadius = 15;
    self.texView.keyboardType = UIKeyboardTypeDefault;
    self.texView.layer.masksToBounds = YES;
    self.texView.layer.borderWidth = 1;
    self.texView.layer.borderColor = [UIColor grayColor].CGColor;
    self.texView.font = [UIFont systemFontOfSize:18];
    self.texView.backgroundColor = [UIColor whiteColor];
    self.texView.scrollEnabled = NO;
    self.texView.textColor = [UIColor blackColor];
    self.texView.autoresizesSubviews = UIViewAutoresizingFlexibleHeight;
    self.texView.text = [DataBaseHandel shareBaseData].dataText;
    [self.commentView addSubview:self.texView];
    
    self.publish = [UIButton buttonWithType:UIButtonTypeCustom];
    //    publish.backgroundColor = [UIColor orangeColor];
    self.publish.frame = CGRectMake(WIDTH - 100, self.texView.frame.origin.y + self.texView.frame.size.height +10, 80, 30);
    [self.publish setTitle:@"发布评论" forState:UIControlStateNormal];
    [self.publish setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.publish addTarget:self action:@selector(publishingAction) forControlEvents:UIControlEventTouchUpInside];
    [self.commentView addSubview:self.publish];
}

//发布资讯评论
-(void)publishCommentAction:(UIButton *)button
{
    NSLog(@"发布评论    %ld",button.tag);
    OConsult *con = self.infoArray[button.tag-10000];
    self.OAddinfoComment.inforId = con.informatinId;
    
    NSLog(@"+++++%ld",self.OAddinfoComment.inforId);
    
    [UIView animateWithDuration:0.1 animations:^{
        self.commentView.frame = CGRectMake(0, 0, self.commentView.frame.size.width, self.commentView.frame.size.height);
        self.publish.tag = button.tag;
    }];
    self.open = YES;
}

//确认发布
-(void)publishingAction
{
    NSLog(@"确认发布");
    self.OAddinfoComment.content = self.texView.text;
//    self.OAddinfoComment.inforId = self.OGetinfor.informatinId;
    NSLog(@"text = %ld  id = %ld",self.OGetinfor.informatinId,self.OAddinfoComment.inforId);
    [self.OAddinfoComment request];
    
    [UIView animateWithDuration:0.1 animations:^{
        self.commentView.frame = CGRectMake(0, HEIGHT, self.commentView.frame.size.width, self.commentView.frame.size.height);
        
        
    }];
    self.open = NO;
    
}


//轻拍关闭
-(void)closeAction
{
    [UIView animateWithDuration:0.1 animations:^{
        self.commentView.frame = CGRectMake(0, HEIGHT, self.commentView.frame.size.width, self.commentView.frame.size.height);
        
    }];
    self.open = NO;
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"view Will Appear");
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self.tableView reloadData];
    
}

//点击图片传对象
-(void)showImage:(OConsult *)con
{
    OPhotoAlbumController *view = [[OPhotoAlbumController alloc]init];
    view.con = con;
    [self.navigationController pushViewController:view animated:YES];
}


//协议方法  push到个人页面
-(void)pushPerson
{
    OPersonViewController *view = [[OPersonViewController alloc]init];
    //下一页面隐藏tabbar  必须在上一级页面的点击时间里写
    view.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:view animated:YES];
    
}
//协议方法点击媒体头像 push 媒体
-(void)pushMP4
{
    NSLog(@"mp4");
    OMainCommentController *view = [[OMainCommentController alloc]init];
    [self.navigationController pushViewController:view animated:YES];
}

//点赞
-(void)agreeAction
{
//    BOOL agree;
//        if (agree == YES) {
    self.goodNum.text = [NSString stringWithFormat:@"%ld",++self.agreeNum];
//            agree = NO;
//        }
}



@end
